﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using NuGet;

namespace Calimbra.Addin.Functions
{
    public class Add_calimbra_references
    {
        public void Process()
        {
            IFileSystem fileSystem = CreateFileSystem();
            PackageManager packageManager = CreatePackageManager(fileSystem);
            string packageId = "calimbra.runtime";
            var version = new SemanticVersion(1,0,0,0);

            bool result = InstallPackage(packageManager, fileSystem, packageId, version);
            if (!result)
            {
                Console.WriteLine("InstallCommandPackageAlreadyExists, packageId");
            }
        }
        private bool InstallPackage(PackageManager packageManager, IFileSystem fileSystem, string packageId, SemanticVersion version)
        {
            if (IsPackageInstalled(packageId, version, packageManager, fileSystem))
            {
                // Use a fast check to verify if the package is already installed. We'll do this by checking if the package directory exists on disk.
                return false;
            }
          
                var installedPackage = packageManager.LocalRepository.FindPackage(packageId);
                if (installedPackage != null)
                {
                    if (version != null && installedPackage.Version >= version)
                    {
                        // If the package is already installed (or the version being installed is lower), then we do not need to do anything. 
                        return false;
                    }
                    else if (packageManager.SourceRepository.Exists(packageId, version))
                    {
                        // If the package is already installed, but
                        // (a) the version we require is different from the one that is installed, 
                        // (b) side-by-side is disabled
                        // we need to uninstall it.
                        // However, before uninstalling, make sure the package exists in the source repository. 
                        packageManager.UninstallPackage(installedPackage, forceRemove: false, removeDependencies: true);
                    }
                }
            
            packageManager.InstallPackage(packageId,version);
            return true;
        }
        private static bool IsPackageInstalled(string packageId, SemanticVersion version, PackageManager packageManager, IFileSystem fileSystem)
        {
            var packageDir = packageManager.PathResolver.GetPackageDirectory(packageId, version);
            var packageFile = packageManager.PathResolver.GetPackageFileName(packageId, version);

            string packagePath = Path.Combine(packageDir, packageFile);

            return fileSystem.FileExists(packagePath);
        }
        protected virtual PackageManager CreatePackageManager(IFileSystem fileSystem, bool useMachineCache = false)
        {
            IPackageRepository repository = new DataServicePackageRepository(new Uri("https://go.microsoft.com/fwlink/?LinkID=230477"));
            var pathResolver = new DefaultPackagePathResolver(fileSystem);
            var packageManager = new PackageManager(repository, pathResolver, fileSystem);
            return packageManager;
        }
        protected virtual IFileSystem CreateFileSystem()
        {
            string installPath = Directory.GetCurrentDirectory();
            return new PhysicalFileSystem(installPath);
        }
       
    }
}
